D:\a\tools.proto\tools.proto\compiler\src\gen\rust\message\from_bytes.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::util::types::TypePathMap; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::message::Templates; |
33 | | use crate::gen::base::message_from_bytes::generate; |
34 | | use crate::gen::base::Error; |
35 | | use crate::gen::codec::CodecMap; |
36 | | use crate::gen::rust::util::{gen_where_clause, RustUtils}; |
37 | | use crate::gen::template::Template; |
38 | | use itertools::Itertools; |
39 | | |
40 | | const TEMPLATE: &[u8] = include_bytes!("from_bytes.template"); |
41 | | |
42 | 28 | pub fn gen_message_from_slice_impl( |
43 | 28 | msg: &Message, |
44 | 28 | codec_map: &CodecMap, |
45 | 28 | type_path_map: &TypePathMap, |
46 | 28 | ) -> Result<String, Error> { |
47 | 28 | let type_path_map = TypePathMapper::new(type_path_map, DefaultTypeMapper); |
48 | 28 | let mut templates = Templates { |
49 | 28 | template: Template::compile(TEMPLATE).unwrap(), |
50 | 28 | codec_map, |
51 | 28 | }; |
52 | 28 | let where_clauses = msg |
53 | 28 | .fields |
54 | 28 | .iter() |
55 | 49 | .map(|field| gen_where_clause(&templates.template, field, &type_path_map, "impl")) |
56 | 28 | .join(""); |
57 | 28 | templates |
58 | 28 | .template |
59 | 28 | .var("generics", RustUtils::get_generics(msg, &type_path_map).to_string()) |
60 | 28 | .var("where_clauses", where_clauses); |
61 | 28 | generate::<RustUtils, _>(templates, msg, &type_path_map, "impl") |
62 | 28 | } |